<template>
  <el-dialog
    :close-on-click-modal="false"
    title="检查考核"
    :visible.sync="show"
    width="1000px"
    center
  >
    <el-form ref="form" :rules="rules" style="padding-top: 10px;" :model="form" label-width="100px">
      <div class="com-form-title">基础信息</div>
      <el-row>
        <el-col :span="12">
          <el-form-item label="项目名称" prop="projectName">
            <el-input v-model="form.projectName" :disabled="disableForm" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-row>
            <el-col :span="12">
              <el-form-item label="考核年份" prop="chkYear">
                <el-date-picker v-model="form.chkYear" style="width:100%" type="year" :disabled="disableForm" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="考核类型" prop="chkType">
                <el-select v-model="form.chkType" :disabled="disableForm" style="width: 100%;" placeholder="">
                  <el-option v-for="item in checkTypes" :key="item.name" :label="item.name" :value="item.value" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </el-col>
        <el-col :span="12">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" :disabled="disableForm" type="textarea" />
          </el-form-item>
        </el-col>
      </el-row>

      <div class="com-form-title">考核结果</div>
      <div class="btns" style="padding-bottom: 10px;">
        <el-button type="primary" @click="addRow">新增行</el-button>
        <el-button type="primary" @click="delCheckDataItem">删除行</el-button>
      </div>
      <com-table
        v-model="selectRow"
        :no-page="true"
        :columns="columns"
        highlight-current-row
        :data="checkDataList"
      >
        <template v-slot:unit="{row}">
          <el-input v-model="row.unit" />
        </template>
        <template v-slot:score="{row}">
          <el-input v-model="row.score" />
        </template>
        <template v-slot:cond="{row}">
          <el-input v-model="row.cond" />
        </template>
      </com-table>
      <!-- <el-form-item label="文件上传">
        <uploader v-model="fileList" :disabled="disableForm" :can-download="optType==='detail'" />
      </el-form-item> -->
      <el-button style="margin-top:10px;" type="primary" @click="showFileModal = true">附件({{ fileList.length }})</el-button>
      <file-uploader v-model="fileList" :show-modal.sync="showFileModal" :disabled="disableForm" :can-download="optType==='detail'" />
    </el-form>
    <el-dialog
      title="新增考核结果"
      :visible.sync="showAddModal"
      width="540px"
      center
      append-to-body
    >
      <el-form ref="checkForm" :rules="checkRules" style="padding-top: 10px;" :model="checkDataItem" label-width="110px">
        <el-form-item label="单位名称" prop="unit">
          <el-input v-model="checkDataItem.unit" />
        </el-form-item>
        <el-form-item label="考核得分" prop="score">
          <el-input v-model="checkDataItem.score" />
        </el-form-item>
        <el-form-item label="考核兑现情况" prop="cond">
          <el-input v-model="checkDataItem.cond" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelAddCheck">取 消</el-button>
        <el-button type="success" icon="el-icon-plus" @click="addCheck">确 定</el-button>
      </span>
    </el-dialog>
    <span slot="footer" class="dialog-footer">
      <el-button @click="show = false">取 消</el-button>
      <el-button type="primary" @click="saveDoc">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { updateCheckAppraisalInfo, getCheckAppraisalInfoDetail, addCheckAppraisalInfo } from '@/api/qualityManage'
import { getAreaList } from '@/api/common'
import { checkTypes } from '@/utils/dicData'
// import uploader from '@/components/Uploader'
import fileUploader from '@/components/FileUploader'
import moment from 'moment'
export default {
  name: 'AddCheckInspect',
  components: {
    // uploader,
    fileUploader
  },
  props: {
    showModal: {
      type: Boolean,
      default: false
    },
    optType: {
      type: String,
      default: 'add'
    },
    dir: {
      type: Object,
      default() {
        return {}
      }
    },
    data: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      showFileModal: false,
      selectRow: null,
      showAddModal: false,
      checkDataList: [],
      checkTypes,
      columns: [
        {
          label: '被考核单位',
          slot: 'unit',
          width: '300'
        },
        {
          label: '考核得分',
          slot: 'score',
          width: '160'
        },
        {
          label: '考核情况',
          slot: 'cond'
        }
      ],
      addType: '', // same   under
      visible: false,
      showAdd: false,
      selectedNode: null,
      checkDataItem: {

      },
      form: {
        projectName: '',
        chkYear: '',
        chkType: '',
        remark: ''
      },
      fileList: [
      ],
      rules: {
        projectName: [
          { required: true, message: '请输入项目名称', trigger: 'change' }
        ],
        chkYear: [
          { required: true, message: '请选择考核年份', trigger: 'change' }
        ],
        chkType: [
          { required: true, message: '请选择考核类型', trigger: 'change' }
        ]
      },
      checkRules: {
        unit: [
          { required: true, message: '请输入被考核单位', trigger: 'change' }
        ],
        score: [
          { required: true, message: '请输入考核得分', trigger: 'change' }
        ],
        cond: [
          { required: true, message: '请输入考核兑现情况', trigger: 'change' }
        ]
      },
      provinceData: [],
      cityData: []
    }
  },
  computed: {
    disableForm() {
      return this.optType === 'detail'
    },
    show: {
      get() {
        return this.showModal
      },
      set(val) {
        this.$emit('update:showModal', val)
      }
    }
  },
  watch: {
    showModal(val) {
      if (val && this.optType !== 'add') {
        this.getCheckAppraisalInfoDetail(this.data.id)
        this.$nextTick(() => {
          Object.keys(this.form).forEach(key => {
            const source = this.data[key]
            if (typeof source === 'boolean') {
              this.form[key] = source ? 1 : 0
            } else {
              this.form[key] = source
            }
          })
        })
      } else if (!val) {
        this.fileList = []
        this.checkDataList = []
        this.$refs.form.resetFields()
      }
    }
  },
  mounted() {
    // this.getRegionData()
  },
  methods: {
    addRow() {
      this.checkDataList = [
        ...this.checkDataList,
        {
          unit: '',
          score: '',
          cond: ''
        }
      ]
    },
    addCheck() {
      this.$refs.checkForm.validate((valid) => {
        if (valid) {
          const item = JSON.parse(JSON.stringify(this.checkDataItem))
          this.checkDataList.push(item)
          this.showAddModal = false
          this.$refs.checkForm.resetFields()
        }
      })
    },
    cancelAddCheck() {
      this.showAddModal = false
    },
    delCheckDataItem() {
      if (this.selectRow) {
        const index = this.checkDataList.indexOf(this.selectRow)
        this.checkDataList.splice(index, 1)
      }
    },
    provinceChange(e) {
      const selectedData = this.provinceData.filter(val => val.name === e)[0]
      if (selectedData) {
        this.form.city = ''
        const params = {
          parentCode: selectedData.code,
          areaType: 2
        }
        getAreaList(params).then(res => {
          this.cityData = res.data || []
        })
      }
    },
    getCheckAppraisalInfoDetail(id) {
      getCheckAppraisalInfoDetail({ checkAppraisalInfoId: id }).then(res => {
        const { lstAttachment, lstCheckAppraisalDetail } = res.data || {}
        if (lstAttachment && lstAttachment.length > 0) {
          this.fileList = lstAttachment
        }
        if (lstCheckAppraisalDetail && lstCheckAppraisalDetail.length > 0) {
          this.checkDataList = lstCheckAppraisalDetail
        }
      })
    },
    getRegionData() {
      getAreaList({ areaType: '1' }).then(res => {
        this.provinceData = res.data || []
      })
    },
    saveDoc() {
      if (this.optType === 'detai') {
        this.show = false
        return
      }
      this.$refs.form.validate((valid) => {
        if (valid) {
          const params = this.optType === 'add' ? {
            checkAppraisalInfo: {
              ...this.form,
              chkYear: moment(this.form.chkYear).format('YYYY')
            },
            lstCheckAppraisalDetail: this.checkDataList,
            lstAttachment: this.fileList
          } : {
            checkAppraisalInfo: {
              ...this.form,
              chkYear: moment(this.form.chkYear).format('YYYY'),
              id: this.data.id
            },
            lstCheckAppraisalDetail: this.checkDataList.map(item => {
              return {
                ...item,
                checkAppraisalInfoId: this.data.id
              }
            }),
            lstAttachment: this.fileList
          }
          const fn = this.optType === 'add' ? addCheckAppraisalInfo : updateCheckAppraisalInfo
          fn(params).then(res => {
            if (res.code === '0001') {
              this.$message({
                type: 'success',
                message: '保存成功!'
              })
              this.show = false
              this.$emit('freshList')
              this.$refs.form.resetFields()
            } else {
              this.$message({
                type: 'error',
                message: res.msg
              })
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.com-form-title{
  font-size: 14px;
  font-weight: bold;
  line-height: 35px;
  color: #409EFF;
  border-bottom: 1px solid #dedede;
  margin-bottom: 6px;
}
</style>
